package com.msb.mall.producet.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.msb.mall.producet.entity.AttrEntity;
import com.msb.mall.producet.service.AttrAttrgroupRelationService;
import com.msb.mall.producet.service.AttrService;
import com.msb.mall.producet.service.CategoryService;
import com.msb.mall.producet.vo.AttrGroupRelationVO;
import com.msb.mall.producet.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.msb.mall.producet.entity.AttrGroupEntity;
import com.msb.mall.producet.service.AttrGroupService;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;



/**
 * 属性分组
 *
 * @author zhangjiajun
 * @email 1606516025@qq.com
 * @date 2025-05-08 21:19:06
 */
@RestController
@RequestMapping("/product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        //根据三级分类的编号获取对应的属性组和属性组的属性信息
        List<AttrGroupWithAttrsVO> list = attrGroupService.getAttrgroupWithAttrsByCAtelogId(catelogId);
        return R.ok().put("data", list);
    }


    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrGroupRelationVO> vos) {
        relationService.save(vos);
        return R.ok();
    }


    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,
                                  @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils pageUtils = attrService.getNoAttrRelation(params, attrgroupId);
        return R.ok().put("page", pageUtils);
    }


    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRelationVO[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }


    @GetMapping("/{attrgroupId}/attr/relation")
     public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);

         return R.ok().put("data", list);
     }

    /**
     * 列表
     * {
     *     page=1, // 当前页码
     *     limit=10, // 每页显示记录数
     *     sidx="id", // 排序字段
     *     order="asc" // 排序方式
     *     key="关键字" // 搜索关键字
     * }
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        //根据找打ode属性组对应的分类id,然后找到对应分类id的一级二级id'
        Long catelogId = attrGroup.getCatelogId();
        Long[] paths = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(paths);


        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
